import csv
import fitz  # PyMuPDF
from file_utils import list_files ,calculate_md5,create_folder
def get_pdf_page_count(pdf_path):
    try:
        pdf_document = fitz.open(pdf_path)
        page_count = pdf_document.page_count
        pdf_document.close()
        return page_count
    except Exception as e:
        print(f"Error: {e}")
        return None

# 已有的数据
existing_data = ["fileName", "MD5"]

# CSV文件路径
csv_file_path = "output.csv"

# 读取CSV文件，找到 "John Doe" 的数据，然后追加 "Engineer"
with open(csv_file_path, mode='r', encoding='utf-8', newline='') as file:
    # 创建CSV读取对象
    csv_reader = csv.reader(file)

    # 将数据读取为列表
    data_list = list(csv_reader)

    # # 查找 "John Doe" 的数据，并追加 "Engineer"
    # for row in data_list:
    #     if row[:2] == existing_data:
    #         row.append("page_count")
    for row in data_list[1:]:
        file_name = row[0]
        file_pdf_path = fr"D:\Users\65720\Desktop\电信项目-方案\文艺\文蚌\文蚌\{file_name}"
        # file_pdf_path = fr"D:\Users\65720\Desktop\电信项目-方案\文艺\勇罕\勇罕\{file_name}"
        page_count = get_pdf_page_count(file_pdf_path)
        if page_count is not None:
        # 将页数添加到行的末尾
            row.append(page_count)
        else:
            print(f"Failed to get the page count for {file_name}.")


# 将更新后的数据写回CSV文件
with open(csv_file_path, mode='w', encoding='utf-8', newline='') as file:
    # 创建CSV写入对象
    csv_writer = csv.writer(file)

    # 将更新后的数据写回CSV文件
    csv_writer.writerows(data_list)

print("Data updated successfully.")